package org.hylexus.hadoop.hadoop_2_test.mr.ii;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class InverseIndex {
	public static void main(String[] args) throws Exception {

		Job job = Job.getInstance(new Configuration());
		job.setJarByClass(InverseIndex.class);

		// mapper
		job.setMapperClass(IndexMapper.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);

		FileInputFormat.setInputPaths(job, new Path(args[0]));

		// combiner
		job.setCombinerClass(IndexCombiner.class);

		// reducer
		job.setReducerClass(IndexReducer.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);

		FileOutputFormat.setOutputPath(job, new Path(args[1]));

		job.waitForCompletion(true);

	}

	public static class IndexMapper extends Mapper<LongWritable, Text, Text, Text> {

		// "hello:a.txt"->1
		// "tom:a.txt"->1
		@Override
		protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
				throws IOException, InterruptedException {
			if (value == null)
				return;
			InputSplit inputSplit = context.getInputSplit();
			FileSplit fsp = (FileSplit) inputSplit;
			String fileName = fsp.getPath().toString();
			String line = value.toString();
			String[] words = line.split(" ");
			for (String w : words) {
				String k = w + "->" + fileName;
				context.write(new Text(k), new Text("1"));
			}
		}

	}

	public static class IndexCombiner extends Reducer<Text, Text, Text, Text> {
		private Text k = new Text();
		private Text v = new Text();

		@Override
		protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)
				throws IOException, InterruptedException {
			if (values == null)
				return;
			long sum = 0;

			String[] wordAndPath = key.toString().split("->");
			String word = wordAndPath[0];
			String path = wordAndPath[1];
			for (Text c : values) {
				sum += Long.parseLong(c.toString().trim());
			}
			k.set(word);
			v.set(path + ":" + sum);
			context.write(k, v);
		}
	}

	public static class IndexReducer extends Reducer<Text, Text, Text, Text> {

		private Text v = new Text();

		@Override
		protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)
				throws IOException, InterruptedException {
			String result = "";
			for (Text t : values) {
				result += "\t" + t.toString();
			}
			v.set(result);
			context.write(key, v);
		}

	}
}
